#SingleInstance force
CoordMode, Mouse, Screen

; Length of a cycle
cycleLength = 50

; Amount of cycles to wait before engaging the hotcorner
delayBeforeActivating = 4


if !fileexist("Hotcorners.ini")
{
	IniWrite, 4, Hotcorners.ini, Settings, delayBeforeActivating
	IniWrite, !1, Hotcorners.ini, Settings, hotkeyTopLeft
	IniWrite, !2, Hotcorners.ini, Settings, hotkeyTopRight
	IniWrite, !3, Hotcorners.ini, Settings, hotkeyBottomLeft
	IniWrite, !4, Hotcorners.ini, Settings, hotkeyBottomRight
}

IniRead, delayBeforeActivating, Hotcorners.ini, Settings, delayBeforeActivating, 4
IniRead, hotkeyTopLeft, Hotcorners.ini, Settings, hotkeyTopLeft, !1
IniRead, hotkeyTopRight, Hotcorners.ini, Settings, hotkeyTopRight, !2
IniRead, hotkeyBottomLeft, Hotcorners.ini, Settings, hotkeyBottomLeft, !3
IniRead, hotkeyBottomRight, Hotcorners.ini, Settings, hotkeyBottomRight, !4

Menu,tray,NoStandard
Menu,tray,icon,Hotcorners.ico
Menu,tray,add,About,About
Menu,tray,add,Settings,Settings
Menu,tray,add,Exit,cleanup



delayBeforeActivating_temp = 0


Loop
{
	cornerWaiting = 0
	cornerActive = 0
	
	MouseGetPos, mouseX, mouseY
	screenWidth := A_ScreenWidth - 1
	screenHeight := A_ScreenHeight - 1

	; Top-left corner
	if mouseX = 0
	{
		if mouseY = 0
		{
			delayBeforeActivating_temp++
			cornerWaiting = 1
		}
	}

	; Top-right corner
	if mouseX = %screenWidth%
	{
		if mouseY = 0
		{
			delayBeforeActivating_temp++
			cornerWaiting = 2
		}
	}

	; Bottom-left corner
	if mouseX = 0
	{
		if mouseY = %screenHeight%
		{
			delayBeforeActivating_temp++
			cornerWaiting = 3
		}
	}

	; Bottom-right corner
	if mouseX = %screenWidth%
	{
		if mouseY = %screenHeight%
		{
			delayBeforeActivating_temp++
			cornerWaiting = 4
		}
	}

	if delayBeforeActivating_temp >= %delayBeforeActivating%
	{
		delayBeforeActivating_temp = 0
		
		if cornerWaiting > 0
		{
			cornerActive = %cornerWaiting%

			if cornerActive = 1
			{
				Send, %hotkeyTopLeft%
			}
			else if cornerActive = 2
			{
				Send, %hotkeyTopRight%
			}
			else if cornerActive = 3
			{
				Send, %hotkeyBottomLeft%
			}
			else if cornerActive = 4
			{
				Send, %hotkeyBottomRight%
			}
		}
	}
	
	if cornerWaiting = 0
	{
		delayBeforeActivating_temp = 0
	}

	;ToolTip, %delayBeforeActivating_temp% - %cornerWaiting% - %cornerActive% - mouse: (%mouseX%`,%mouseY%) - screen: (%screenWidth%`,%screenHeight%) - raw: (%A_ScreenWidth%`,%A_ScreenHeight%) - %cycleLength%`,%cycleLength_raw%`,%delayBeforeActivating%

	Sleep, %cycleLength%
}





; GUI for user configurable hotkeys

settings:
	winKey = #
	
	cycleLength_raw := cycleLength * delayBeforeActivating
	
	gui 7: add,text,,Delay in milliseconds before activating a hotcorner (default: 200)
	gui 7: add,edit,w80 number vcycleLength_raw, %cycleLength_raw%
	
	; Top left
	haswinHotkeyTopLeft=UnChecked
	if InStr(hotkeyTopLeft, winKey)
	{
		haswinHotkeyTopLeft=Checked
		StringReplace , cleanHotkeyTopLeft, hotkeyTopLeft, # , , All
	}
	else
	{
		cleanHotkeyTopLeft = %hotkeyTopLeft%
	}
	gui 7: add,text,,Select a hotkey for your top left hotcorner
	gui 7: add,hotkey,vnewHotkeyTopLeft, %cleanHotkeyTopLeft%
	gui 7: add,checkbox,vsuperHotkeyTopLeft %haswinHotkeyTopLeft%, + Windows Key

	; Top right
	haswinHotkeyTopRight=UnChecked
	if InStr(hotkeyTopRight, winKey)
	{
		haswinHotkeyTopRight=Checked
		StringReplace , cleanHotkeyTopRight, hotkeyTopRight, # , , All
	}
	else
	{
		cleanHotkeyTopRight = %hotkeyTopRight%
	}
	gui 7: add,text,,Select a hotkey for your top right hotcorner
	gui 7: add,hotkey,vnewHotkeyTopRight, %cleanHotkeyTopRight%
	gui 7: add,checkbox,vsuperHotkeyTopRight %haswinHotkeyTopRight%, + Windows Key

	; Bottom left
	haswinHotkeyBottomLeft=UnChecked
	if InStr(hotkeyBottomLeft, winKey)
	{
		haswinHotkeyBottomLeft=Checked
		StringReplace , cleanHotkeyBottomLeft, hotkeyBottomLeft, # , , All
	}
	else
	{
		cleanHotkeyBottomLeft = %hotkeyBottomLeft%
	}
	gui 7: add,text,,Select a hotkey for your bottom left hotcorner
	gui 7: add,hotkey,vnewHotkeyBottomLeft, %cleanHotkeyBottomLeft%
	gui 7: add,checkbox,vsuperHotkeyBottomLeft %haswinHotkeyBottomLeft%, + Windows Key

	; Bottom right
	haswinHotkeyBottomRight=UnChecked
	if InStr(hotkeyBottomRight, winKey)
	{
		haswinHotkeyBottomRight=Checked
		StringReplace , cleanHotkeyBottomRight, hotkeyBottomRight, # , , All
	}
	else
	{
		cleanHotkeyBottomRight = %hotkeyBottomRight%
	}
	gui 7: add,text,,Select a hotkey for your bottom right hotcorner
	gui 7: add,hotkey,vnewHotkeyBottomRight, %cleanHotkeyBottomRight%
	gui 7: add,checkbox,vsuperHotkeyBottomRight %haswinHotkeyBottomRight%, + Windows Key


	gui 7: add,button,gsavesettings,Save	
	gui 7: show
return


7guiclose:
	gui 7: destroy
return

savesettings:
	gui 7: submit
	gui 7: destroy
	
	delayBeforeActivating := round(cycleLength_raw / 50)
	IniWrite, %delayBeforeActivating%, Hotcorners.ini, Settings, delayBeforeActivating

	; Top left
	if superHotkeyTopLeft = 1
	{
		newHotkeyTopLeft = #%newHotkeyTopLeft%
	}
	if newHotkeyTopLeft != %hotkeyTopLeft%
	{
		hotkeyTopLeft = %newHotkeyTopLeft%
		IniWrite, %hotkeyTopLeft%, Hotcorners.ini, Settings, hotkeyTopLeft
	}

	; Top right
	if superHotkeyTopRight = 1
	{
		newHotkeyTopRight = #%newHotkeyTopRight%
	}
	if newHotkeyTopRight != %hotkeyTopRight%
	{
		hotkeyTopRight = %newHotkeyTopRight%
		IniWrite, %hotkeyTopRight%, Hotcorners.ini, Settings, hotkeyTopRight
	}

	; Bottom left
	if superHotkeyBottomLeft = 1
	{
		newHotkeyBottomLeft = #%newHotkeyBottomLeft%
	}
	if newHotkeyBottomLeft != %hotkeyBottomLeft%
	{
		hotkeyBottomLeft = %newHotkeyBottomLeft%
		IniWrite, %hotkeyBottomLeft%, Hotcorners.ini, Settings, hotkeyBottomLeft
	}

	; Bottom right
	if superHotkeyBottomRight = 1
	{
		newHotkeyBottomRight = #%newHotkeyBottomRight%
	}
	if newHotkeyBottomRight != %hotkeyBottomRight%
	{
		hotkeyBottomRight = %newHotkeyBottomRight%
		IniWrite, %hotkeyBottomRight%, Hotcorners.ini, Settings, hotkeyBottomRight
	}

return

CleanUp:
ExitApp
return

About:
MsgBox, Created by Polatrite`nCopyright 2010`n`nIndependent game developer and software programmer`nVisit my site for more tools and games!`nhttp://www.polatrite.com/



#r::
Reload
return

